package com.sichen.product.web;

import com.sichen.product.entity.CategoryEntity;
import com.sichen.product.service.CategoryService;
import com.sichen.product.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 商城首页的跳转Controller
 * <p>Created with IntelliJ IDEA.
 * <p>Author: 思尘
 * <p>Created time: 2022-09-13 15:21
 * <p>Version: V1.0
 */
@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @GetMapping({"/", "index.html"})
    public String indexPage(Model model){

        // 1.查出所有的1级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevelOneCategorys();
        model.addAttribute("categorys",categoryEntities);
        return "index";
        // 这里不用指定文件的路径和后缀 ,
        // thymeleaf有一个默认的前缀和后缀
        // prefix : "classpath:/templates/"
        // suffix : ".html"
        // 使用视图解析器进行拼串 , 拼接前缀和后缀
    }

    /**
     * 获取首页分类数据
     * @return 返回封装后的分类数据
     */
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatalogJson(){
        return categoryService.getCatalogJson();
    }
}
